package com.ceshiren.chnjx.wework.page;

import com.ceshiren.chnjx.wework.common.Constant;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * @author: JJJJ
 * @date:2021/11/2 17:06
 * @Description: TODO
 */
public class BasePage {

    public WebDriver driver;

    public BasePage(WebDriver driver) {
        this.driver = driver;
    }
    public BasePage(){

    }

/**
     * 等待元素可见
     * @param by 元素定位表达是
     * @return 所定位元素
     */
    public WebElement waitElementVisible(By by){
        WebDriverWait webDriverWait = new WebDriverWait(driver,Duration.ofSeconds(Constant.WAIT_TIMEOUT));
        WebElement webElement = null;
        try{
            webElement = webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(by));
        }catch (Exception e){
            // todo： 其他异常处理
            if (i++ < 3){
                // 处理弹框
                waitAlertVisibleAndAccept();
                waitElementVisible(by);
            }
            logger.error("找不到元素："+ by);
            e.printStackTrace();
        }
        return webElement;
    }

    /**
     * 等待元素可被点击
     * @param by 元素定位表达是
     * @return
     */
    public WebElement waitElementClickable(By by){
        WebDriverWait webDriverWait = new WebDriverWait(driver,Duration.ofSeconds(Constant.WAIT_TIMEOUT));
        WebElement webElement = null;
        try{
            webElement = webDriverWait.until(ExpectedConditions.elementToBeClickable(by));
        }catch (Exception e){
            // todo： 其他异常处理
            if (i++ < 3){
                // 处理弹框
                waitAlertVisibleAndAccept();
                waitElementClickable(by);
            }
            logger.error("找不到元素："+ by);
            e.printStackTrace();
        }
        return webElement;
    }
    
    /**
     * 弹框处理
     */
    public void waitAlertVisibleAndAccept(){
            try {
                WebDriverWait webDriverWait = new WebDriverWait(driver,Duration.ofSeconds(2));
                webDriverWait.until(ExpectedConditions.alertIsPresent());
                driver.switchTo().alert().accept();
            } catch (Exception e) {
                logger.info("没找到弹框");
            }
        }

    /**
     * 输入数据
     * @param by 元素by对象
     * @param inputData 输入的数据
     */
    public void sendData(By by,String inputData){
        waitElementVisible(by).clear();
        waitElementVisible(by).sendKeys(inputData);
    }

    /**
     * 输入按键操作
     */
    public void typeKey(By by, Keys keys){
        waitElementVisible(by).sendKeys(keys);
    }

    /**
     * 点击元素的
     * @param by
     */
    public void clickElement(By by){
        waitElementVisible(by).click();
    }

    /**
     * 获取元素文本值
     * @param by
     */
    public String getElementText(By by){
        //TODO - 统一日志处理
        String text = waitElementVisible(by).getText();
        return text;
    }

    /**
     * 获取元素属性值
     * @param by 元素定位表达是
     * @param attributeName  元素的属性名
     */
    public String getElementAttribute(By by, String attributeName){
        String value = waitElementVisible(by).getAttribute(attributeName);
        return value;
    }

    /**
     * 元素是否可见
     * @param by
     * @return
     */
    public Boolean isElementDisplayed(By by){
        return waitElementVisible(by).isDisplayed();
    }


    public void switchToFrame(String frameId){
        driver.switchTo().frame(frameId);
    }

    public void switchToFrame(By by){
        driver.switchTo().frame(waitElementVisible(by));
    }

    public void switchToParentFrame(){
        driver.switchTo().parentFrame();
    }
}
